<!-- 我的按钮 -->

<template>
  <div>
    <BtnPrimary :disabled="disabled" @click="toAdd">
      <i class="el-icon-arrow-right" />
    </BtnPrimary>

    <BtnPrimary :disabled="disabled" @click="toDel">
      <i class="el-icon-arrow-left" />
    </BtnPrimary>
  </div>
</template>

<script>
// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'

// ============================== 导出组件 ============================== //

export default {
  /**
   * 名称定义 (实际使用名称)
   */
  name: 'MyBtn',

  /**
   * 混入列表 (配置选项合并)
   */
  mixins: [CORE.mixWeb(), CORE.mixInfo()],

  /**
   * 计算属性 (慎用箭头函数)
   */
  computed: {
    /**
     * 是否禁用
     */
    disabled() {
      const { config = {} } = CORE.componentParent(this, 'WebForm') || {}
      return config.disabled
    },
  },

  /**
   * 本地方法 (轻量重要在前)
   */
  methods: {
    /**
     * 点击添加
     */
    async toAdd() {
      const { INIT_INFO, info, param, list, $nextTick } = this
      const { obj, bind, idKey } = info

      const arr = list.filter((item) => param.idList.some((id) => id === item[idKey]))
      const chosen = this.transChosen(CORE.clone(arr))

      param.idList = param.rowList = []
      CORE.merge(info.left, INIT_INFO.left) // 重置左侧
      await $nextTick()

      if (CORE.isArray(obj[bind])) obj[bind].push(...chosen)
      else this.$set(obj, bind, chosen)

      await $nextTick()
      CORE.merge(info.right, INIT_INFO.right) // 重置右侧

      this.$emit('change', obj[bind])
    },

    /**
     * 转换选中项
     */
    transChosen(chosen) {
      return chosen
    },

    /**
     * 点击移除
     */
    async toDel() {
      const { INIT_INFO, info, param, $nextTick } = this
      const { obj, bind, right, idKey } = info

      obj[bind] = obj[bind].filter((item) => !right.idList.some((id) => id === item[idKey]))
      await $nextTick()

      param.idList = param.rowList = []
      CORE.merge(info.left, INIT_INFO.left) // 重置左侧
      CORE.merge(info.right, INIT_INFO.right) // 重置右侧

      this.$emit('change', obj[bind])
    },
  },
}
</script>
